import java.util.Arrays;
import java.util.Scanner;

//密码加密与解密
public class Test {
    public static void main(String[] args) {
        //密码加密
        System.out.println("请输入数字密码:");
        Scanner scanner = new Scanner(System.in);
        int password = scanner.nextInt();
        //确认密码位数
        int tmp = password;
        int count = 0;
        while (tmp != 0){
            tmp /= 10;
            count++;
        }
        System.out.println("密码个数：" + count);
        //设置密码数组
        int[] arr = new int[count];
        int num = 0;
        for (int i = arr.length - 1; i >= 0; i--) {
            arr[i] = password % 10;
            password /= 10;
        }
        System.out.println("密码数组：" + Arrays.toString(arr));
        //加密
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (arr[i] + 5) % 10;
        }
        System.out.println("加密之后：" + Arrays.toString(arr));
        //倒置
        arr = Inversion(arr);
        System.out.println("倒置之后：" + Arrays.toString(arr));
        //开始解密
        //倒置
        arr = Inversion(arr);
        //解密
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] >= 0 && arr[i] <= 4){
                arr[i] += 10;
            }
            arr[i] -= 5;
        }
        System.out.println("解密之后：" + Arrays.toString(arr));
        //还原密码
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            number = number * 10 + arr[i];
        }
        System.out.println("原密码：" + number);
    }

    public static int[] Inversion(int[] arr){
        for (int i = 0,j = arr.length-1; i < j; i++,j--) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        return arr;
    }

}
